﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Shop.Base.Entity.Assist;
using Shop.Base.Entity.View;
using Shop.Tools;

namespace Shop.Web.Models
{
    public static class SystemHelper
    {
        public static IHtmlContent CreateMenuTree(List<SystemMenuHelp> list, string area, string controller)
        {
            if (list == null || !list.Any()) return new HtmlString("");
            var sb = new StringBuilder();
            foreach (var item in list)
            {
                if (!item.Child.Any())
                {
                    var url = (item.AreaName.IsNullOrEmpty() ? "" : ("/" + item.AreaName)) +
                              (item.ControllerName.IsNullOrEmpty() ? "" : ("/" + item.ControllerName)) +
                              (item.ActionName.IsNullOrEmpty() ? "" : (item.ActionName == "Index" ? "" : "/" + item.ActionName));
                    sb.Append("<li class=\"nav-item\">");
                    sb.Append($"<a href=\"{(url.IsNullOrEmpty() ? "#" : url)}\" class=\"nav-link {(url.ToLower().Contains($"/{area}/{controller}") ? "active" : "")}\">");
                    sb.Append($"<i class=\"{(item.MenuIcon.IsNullOrEmpty() ? "icon icon-speedometer" : item.MenuIcon)}\"></i> {item.MenuName}");
                    sb.Append(" </a></li>");
                }
                else
                {
                    sb.Append("<li class=\"nav-item nav-dropdown\">");
                    sb.Append("<a href=\"#\" class=\"nav-link nav-dropdown-toggle\">");
                    sb.Append($"<i class=\"{(item.MenuIcon.IsNullOrEmpty() ? "icon icon-target" : item.MenuIcon)}\"></i> {item.MenuName} <i class=\"fa fa-caret-left\"></i>");
                    sb.Append("</a>");
                    sb.Append(" <ul class=\"nav-dropdown-items\">");
                    LoadTree(item.Child, sb, area, controller);
                    sb.Append("</ul> </li>");
                }
            }
            return new HtmlString(sb.ToString());
        }

        private static void LoadTree(List<SystemMenuHelp> list, StringBuilder sb, string area, string controller)
        {
            foreach (var item in list)
            {
                if (!item.Child.Any())
                {
                    var url = (item.AreaName.IsNullOrEmpty() ? "" : ("/" + item.AreaName)) +
                              (item.ControllerName.IsNullOrEmpty() ? "" : ("/" + item.ControllerName)) +
                              (item.ActionName.IsNullOrEmpty() ? "" : (item.ActionName == "Index" ? "" : "/" + item.ActionName));
                    sb.Append("<li class=\"nav-item\">");
                    sb.Append($"<a href=\"{(url.IsNullOrEmpty() ? "#" : url)}\" class=\"nav-link {(url.ToLower().Contains($"/{area}/{controller}") ? "active" : "")}\">");
                    sb.Append($"<i class=\"{(item.MenuIcon.IsNullOrEmpty() ? "icon icon-speedometer" : item.MenuIcon)}\"></i> {item.MenuName}");
                    sb.Append(" </a></li>");
                }
                else
                {
                    sb.Append("<li class=\"nav-item nav-dropdown\">");
                    sb.Append("<a href=\"#\" class=\"nav-link nav-dropdown-toggle\">");
                    sb.Append($"<i class=\"{(item.MenuIcon.IsNullOrEmpty() ? "icon icon-target" : item.MenuIcon)}\"></i> {item.MenuName} <i class=\"fa fa-caret-left\"></i>");
                    sb.Append("</a>");
                    sb.Append(" <ul class=\"nav-dropdown-items\">");
                    LoadTree(item.Child, sb, area,controller);
                    sb.Append("</ul> </li>");
                }
            }
        }

        public static IHtmlContent GetButton(List<UserRoleFunction> list, string area, string controller)
        {
            if (area.IsNullOrEmpty()|| controller.IsNullOrEmpty() || list == null || !list.Any(x => !x.AreaStr.IsNullOrEmpty() && x.AreaStr==area && !x.ControllerStr.IsNullOrEmpty() && x.ControllerStr== controller && !x.ActionStr.IsNullOrEmpty() && x.ActionStr=="Index")) return new HtmlString("[]");
            var buttonList = list.Where(x => !x.AreaStr.IsNullOrEmpty() && x.AreaStr == area && !x.ControllerStr.IsNullOrEmpty() && x.ControllerStr == controller && !x.ActionStr.IsNullOrEmpty() && x.ActionStr == "Index").ToList();
            var sb = new StringBuilder();
            sb.Append("{");
            buttonList.ForEach(x => {
                sb.Append("\"" +(x.ButtonName=="新增"&&x.ButtonTag=="Edit"?"Add": x.ButtonTag)  + "\":{ \"buttonName\":\"" + x.ButtonName + "\",\"icon\":\"" + x.ButtonIcons + "\",\"title\":\"" + x.Titles + "\"},");
            });
            return new HtmlString(sb.ToString().TrimEnd(',')+"}");
        }

        /// <summary>
        /// 查询【vue】
        /// </summary>
        /// <param name="click">JavaScript 事件名称</param>
        /// <param name="type">按钮类型</param>
        /// <param name="opt">操作类型</param>
        /// <returns></returns>
        public static IHtmlContent ButtonSearch(string click= "searchs", string type= "primary", string opt= "GetList")
        {
            return Buttons(click,type,opt);
        }
        /// <summary>
        /// 设置检索条件【vue】
        /// </summary>
        /// <param name="click">JavaScript 事件名称</param>
        /// <param name="type">按钮类型</param>
        /// <param name="opt">操作类型</param>
        /// <returns></returns>
        public static IHtmlContent ButtonSetSearch(string click = "funSetParam", string type = "", string opt = "SetSearch")
        {
            return Buttons(click, type, opt, "showParam?'关闭':'打开'");
        }
        /// <summary>
        /// 设置显示列【vue】
        /// </summary>
        /// <param name="click">JavaScript 事件名称</param>
        /// <param name="type">按钮类型</param>
        /// <param name="opt">操作类型</param>
        /// <returns></returns>
        public static IHtmlContent ButtonSetColums(string click = "funSetColumns", string type = "", string opt = "SetColums")
        {
            return Buttons(click, type, opt, "showColumn?'关闭':'打开'");
        }
        /// <summary>
        /// 新增【vue】
        /// </summary>
        /// <param name="click">JavaScript 事件名称</param>
        /// <param name="type">按钮类型</param>
        /// <param name="opt">操作类型</param>
        /// <returns></returns>
        public static IHtmlContent ButtonAdd(string click = "funAdd", string type = "info", string opt = "Add")
        {
            return Buttons(click, type, opt);
        }
        /// <summary>
        /// 编辑【vue】
        /// </summary>
        /// <param name="click">JavaScript 事件名称</param>
        /// <param name="type">按钮类型</param>
        /// <param name="opt">操作类型</param>
        /// <returns></returns>
        public static IHtmlContent ButtonEdit(string click = "funEdit", string type = "success", string opt = "Edit")
        {
            return Buttons(click, type, opt);
        }

        /// <summary>
        /// 删除【vue】
        /// </summary>
        /// <param name="click">JavaScript 事件名称</param>
        /// <param name="type">按钮类型</param>
        /// <param name="opt">操作类型</param>
        /// <returns></returns>
        public static IHtmlContent ButtonDel(string click = "funDel", string type = "danger", string opt = "Delete")
        {
            return Buttons(click, type, opt);
        }
        /// <summary>
        /// 设置菜单【vue】
        /// </summary>
        /// <param name="click">JavaScript 事件名称</param>
        /// <param name="type">按钮类型</param>
        /// <param name="opt">操作类型</param>
        /// <returns></returns>
        public static IHtmlContent ButtonSetFunction(string click = "funSetFunction", string type = "primary", string opt = "SetFunction")
        {
            return Buttons(click, type, opt);
        }

        /// <summary>
        /// 设置菜单按钮【vue】
        /// </summary>
        /// <param name="click">JavaScript 事件名称</param>
        /// <param name="type">按钮类型</param>
        /// <param name="opt">操作类型</param>
        /// <returns></returns>
        public static IHtmlContent ButtonSetButton(string click = "funSetButton", string type = "primary", string opt = "SetButton")
        {
            return Buttons(click, type, opt);
        }

        /// <summary>
        /// 设置角色【vue】
        /// </summary>
        /// <param name="click">JavaScript 事件名称</param>
        /// <param name="type">按钮类型</param>
        /// <param name="opt">操作类型</param>
        /// <returns></returns>
        public static IHtmlContent ButtonSetRole(string click = "funRole", string type = "primary", string opt = "SetRole")
        {
            return Buttons(click, type, opt);
        }

        /// <summary>
        /// 初始化密码【vue】
        /// </summary>
        /// <param name="click">JavaScript 事件名称</param>
        /// <param name="type">按钮类型</param>
        /// <param name="opt">操作类型</param>
        /// <returns></returns>
        public static IHtmlContent ButtonInitPwd(string click = "funInitPwd", string type = "primary", string opt = "InitPwd")
        {
            return Buttons(click, type, opt);
        }
        //showParam?'关闭':'打开'
        private static IHtmlContent Buttons(string click , string type, string opt,string add="")
        {
            if (add.IsNullOrEmpty())
            {
                return new HtmlString($"<el-button size=\"mini\" type=\"{type}\" @click=\"{click}\" v-if=\"buttons.{opt}\" v-bind:icon=\"buttons.{opt}.icon\" v-bind:title=\"buttons.{opt}.title\">{{{{buttons.{opt}.buttonName}}}}</el-button>");
            }
            return new HtmlString($"<el-button size=\"mini\" type=\"{type}\" @click=\"{click}\" v-if=\"buttons.{opt}\" v-bind:icon=\"buttons.{opt}.icon\" v-bind:title=\"buttons.{opt}.title\">{{{{{add}}}}}{{{{buttons.{opt}.buttonName}}}}</el-button>");
        }
    }
}